package coms.action;

import com.opensymphony.xwork2.Action;
import coms.delegates.ConferenceItemDelegate;
import coms.delegates.ConferenceDelegate;
import coms.delegates.ConferenceItemTypeDelegate;
import coms.dto.ConferenceItem;
import coms.dto.Conference;
import coms.dto.ConferenceItemType;
import coms.dto.User;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.JMSException;

public class ConferenceItemAction extends ComsActionSupport implements IAction {

    private int conferenceItemId;
    private ConferenceItem conferenceItem;
    private List<ConferenceItem> conferenceItemList;
    private ConferenceItemDelegate conferenceItemDelegate;
    private List<Conference> conferenceList;
    private ConferenceDelegate conferenceDelegate;
    private List<ConferenceItemType> conferenceItemTypeList;
    private ConferenceItemTypeDelegate conferenceItemTypeDelegate;
    private String action;
    private boolean admin;

    public boolean isAdmin() {
       User u = (User) getSession().get("user");
        if (u != null) {
            admin = u.isAdmin();
        } 
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public ConferenceItemAction() {
        conferenceItem = new ConferenceItem();

        conferenceItemList = new ArrayList<ConferenceItem>();
        conferenceItemDelegate = new ConferenceItemDelegate();

        conferenceList = new ArrayList<Conference>();
        conferenceDelegate = new ConferenceDelegate();

        conferenceItemTypeList = new ArrayList<ConferenceItemType>();
        conferenceItemTypeDelegate = new ConferenceItemTypeDelegate();

    }

    private void initForm() {
        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
        try {
            conferenceList = conferenceDelegate.findAll();
            conferenceItemTypeList = conferenceItemTypeDelegate.findAll();
        } catch (Exception ex) {
            Logger.getLogger(ConferenceItemAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void populateDefault() {
        conferenceItem = new ConferenceItem();

        Date today = Calendar.getInstance().getTime();

        conferenceItem.setStartDate(today);
        conferenceItem.setEndDate(today);
    }

    private void populate() {
        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
        try {
            conferenceItem = conferenceItemDelegate.getById(conferenceItemId);
        } catch (Exception ex) {
            Logger.getLogger(ConferenceItemAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String input() {
        initForm();
        populateDefault();
        action = "createConferenceItem_";
        return "input";
    }

    @Override
    public String edit() {
        initForm();
        populate();
        action = "updateConferenceItem_" + conferenceItem.getId();
        return "edit";
    }

    @Override
    public String create() {
        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
        conferenceItemDelegate.create(conferenceItem);
        return Action.SUCCESS;
    }

    @Override
    public String update() {
        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
        try {
            conferenceItemDelegate.update(conferenceItem);
        } catch (Exception ex) {
            return "error";
        }

        return "updated";
    }

    @Override
    public String delete() {
        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
        populate();
        conferenceItemDelegate.delete(conferenceItem);
        return "deleted";
    }

    @Override
    public String findAll() {
        try {
            conferenceItemList = conferenceItemDelegate.findAll();
        } catch (Exception ex) {
            return "error";
        }
        return Action.SUCCESS;
    }

    @Override
    public String search() {
        initForm();
        try {

            conferenceItemList = conferenceItemDelegate.search(conferenceItem);

        } catch (Exception ex) {
            return "error";
        }
        return "search";
    }

    public ConferenceItem getConferenceItem() {
        return conferenceItem;
    }

    public void setConferenceItem(ConferenceItem conferenceItem) {
        this.conferenceItem = conferenceItem;
    }

    public List<ConferenceItem> getConferenceItemList() {
        return conferenceItemList;
    }

    public void setConferenceItemList(List<ConferenceItem> conferenceItemList) {
        this.conferenceItemList = conferenceItemList;
    }

    public List<Conference> getConferenceList() {
        return conferenceList;
    }

    public void setConferenceList(List<Conference> conferenceList) {
        this.conferenceList = conferenceList;
    }

    public List<ConferenceItemType> getConferenceItemTypeList() {
        return conferenceItemTypeList;
    }

    public void setConferenceItemTypeList(List<ConferenceItemType> conferenceItemTypeList) {
        this.conferenceItemTypeList = conferenceItemTypeList;
    }

    public void setConference(int conferenceId) {
        try {
            Conference conference = conferenceDelegate.getById(conferenceId);
            conferenceItem.setConference(conference);
        } catch (Exception ex) {
            Logger.getLogger(ConferenceItemAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setConferenceItemType(int conferenceItemTypeId) {
        try {
            ConferenceItemType conferenceItemType = conferenceItemTypeDelegate.getById(conferenceItemTypeId);
            conferenceItem.setConferenceItemType(conferenceItemType);
        } catch (Exception ex) {
            Logger.getLogger(ConferenceItemAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getConferenceItemId() {
        return "" + conferenceItemId;
    }

    public void setConferenceItemId(String conferenceItemId) {
        try {
            if (conferenceItemId != null && !conferenceItemId.equals("")) {
                this.conferenceItemId = Integer.parseInt(conferenceItemId);
                conferenceItem.setId(this.conferenceItemId);
            }
        } catch (Exception ex) {
            Logger.getLogger(ConferenceItemAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getAction() {
        Map<String, Object> user = getSession();
        User u = (User) user.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String sendSubmissionDateReminder() throws JMSException {
        populate();
        conferenceItemDelegate.sendReminder(conferenceItem);
        return Action.SUCCESS;
    }

    public String notifyAuthors() throws JMSException {
        populate();
        conferenceItemDelegate.notifyAuthors(conferenceItem);
        return Action.SUCCESS;
    }

    public String calculateReview() {
        conferenceItem.setId(conferenceItemId);
        conferenceItemDelegate.calculateReview(conferenceItem);
        return Action.SUCCESS;
    }
}
